package fr.cephb.dbsnptools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import fr.cephb.io.IOUtils;


public class CompareToDBSNP
	{
	private Pattern delim=Pattern.compile("[\t]");
	private Pattern slash=Pattern.compile("[/]");
	private int colContext=1;
	private int colBase=6;
	private CompareToDBSNP()
		{
		
		}
	public void run (BufferedReader r)
		throws IOException
		{
		String line;
		while((line=r.readLine())!=null)
			{
			String tokens[]=delim.split(line);
			String contexts[]=slash.split(tokens[colContext]);
			String dna;
			if(tokens.length==colBase)//split() remove trailing tabs...
				{
				dna="";
				}
			else
				{
				dna=tokens[colBase];
				}
			boolean ok=false;
			for(String ctx:contexts)
				{
				if( (ctx.equals("-") && dna.isEmpty()) ||
					ctx.equalsIgnoreCase(dna))
					{
					ok=true;
					break;
					}
					
				}
			if(!ok)
				{
				System.out.println(line);
				}
			}
		}
	public static void main(String[] args) {
		try
			{
			CompareToDBSNP app=new CompareToDBSNP();
			int optind=0;
			while(optind< args.length)
				{
				if(args[optind].equals("-h") ||
				   args[optind].equals("-help") ||
				   args[optind].equals("--help"))
					{
					System.err.println("Options:");
					System.err.println(" -h help; This screen.");
					return;
					}
				else if(args[optind].equals("-ctx"))
					{
					app.colContext=Integer.parseInt(args[++optind]);
					}
				else if(args[optind].equals("-base"))
					{
					app.colBase=Integer.parseInt(args[++optind]);
					}
				else if(args[optind].equals("--"))
					{
					optind++;
					break;
					}
				else if(args[optind].startsWith("-"))
					{
					System.err.println("Unknown option "+args[optind]);
					return;
					}
				else 
					{
					break;
					}
				++optind;
				}       
			if(optind==args.length)
                    {
                    app.run(new BufferedReader(new InputStreamReader(System.in)));
                    }
            else
                    {
                    while(optind< args.length)
                            {
                          	java.io.BufferedReader r= IOUtils.openReader(args[optind++]);
                            app.run(r);
                            r.close();
                            }
                    }
			} 
		catch(Throwable err)
			{
			err.printStackTrace();
			}
		}
	}
